package test;

import common.Utils;
import org.openqa.selenium.By;
import org.openqa.selenium.support.ui.ExpectedConditions;

public class SignUpPage extends Utils {
    public static String url = "http://127.0.0.1:58080/sign-up.html";
    public SignUpPage () {
        super(url);
    }
    /**
     * 检查页面是否可以正常打开
     */
    public void checkPageRight() throws InterruptedException {
        // 检查菜单是否加载成功
        driver.findElement(By.cssSelector("#signUpForm > div > h2"));
        driver.findElement(By.cssSelector("#signUpForm > div > div:nth-child(6) > label > span > a"));
        driver.findElement(By.cssSelector("#signUpForm > div > div:nth-child(2) > label"));
        // 检查登录框是否加载成功
        driver.findElement(By.cssSelector("#username"));
        driver.findElement(By.cssSelector("#nickname"));
        driver.findElement(By.cssSelector("#password"));
        driver.findElement(By.cssSelector("#passwordRepeat"));
        driver.findElement(By.cssSelector("#policy"));
        driver.findElement(By.cssSelector("#submit"));
        // 是否可以跳转到登录页
        driver.findElement(By.cssSelector("body > div > div > div.text-center.text-muted.mt-3 > a"));
    }

    /**
     * 异常注册
     * 用户名填写 / 昵称填写 / 密码填写 / 确认密码填写
     * 用户名填写 / 昵称不填写 / 密码填写 / 确认密码不填写
     * 用户名不填写 / 昵称填写 / 密码填写 / 确认密码不填写
     * 用户名不填写 / 昵称不填写 / 密码填写 / 确认密码不填写
     * 用户名填写 / 昵称不填写 / 密码填写 / 确认密码填写
     * 用户名不填写 / 昵称填写 / 密码不填写 / 确认密码填写
     *
     */
    public void SignUpFail (String username,String nickname,String password,String passwordRepeat) {
        // 用户名填写 / 昵称不填写 / 密码填写 / 确认密码不填写
        driver.findElement(By.cssSelector("#username")).sendKeys(username);
        driver.findElement(By.cssSelector("#nickname"));
        driver.findElement(By.cssSelector("#password")).sendKeys(password);
        driver.findElement(By.cssSelector("#passwordRepeat"));
        driver.findElement(By.cssSelector("#policy")).click();
        driver.findElement(By.cssSelector("#submit")).click();

        // 捕获错误
        driver.findElement(By.cssSelector("#signUpForm > div > div:nth-child(3) > div")); // 昵称不能为空
        driver.findElement(By.cssSelector("#signUpForm > div > div:nth-child(5) > div > div")); // 检查确认密码

        // 用户名不填写 / 昵称填写 / 密码填写 / 确认密码不填写
        driver.findElement(By.cssSelector("#username")).clear();
        driver.findElement(By.cssSelector("#nickname")).sendKeys(nickname);
        driver.findElement(By.cssSelector("#password")).sendKeys(password);
        driver.findElement(By.cssSelector("#passwordRepeat"));
        driver.findElement(By.cssSelector("#submit")).click();

        // 捕获错误
        driver.findElement(By.cssSelector("#signUpForm > div > div:nth-child(2) > div")); // 用户名不能为空
        driver.findElement(By.cssSelector("#signUpForm > div > div:nth-child(5) > div > div")); // 检查确认密码

        // 用户名不填写 / 昵称不填写 / 密码填写 / 确认密码不填写
        driver.findElement(By.cssSelector("#nickname")).clear();
        driver.findElement(By.cssSelector("#password")).clear();
        driver.findElement(By.cssSelector("#password")).sendKeys(password);
        driver.findElement(By.cssSelector("#passwordRepeat"));
        driver.findElement(By.cssSelector("#submit")).click();

        // 捕获错误
        driver.findElement(By.cssSelector("#signUpForm > div > div:nth-child(2) > div")); // 用户名不能为空
        driver.findElement(By.cssSelector("#signUpForm > div > div:nth-child(3) > div")); // 昵称不能为空
        driver.findElement(By.cssSelector("#signUpForm > div > div:nth-child(5) > div > div")); // 检查确认密码

        // 用户名填写 / 昵称不填写 / 密码填写 / 确认密码填写
        driver.findElement(By.cssSelector("#username")).sendKeys(username);
        driver.findElement(By.cssSelector("#nickname")).clear();
        driver.findElement(By.cssSelector("#password")).clear();
        driver.findElement(By.cssSelector("#password")).sendKeys(password);
        driver.findElement(By.cssSelector("#passwordRepeat")).sendKeys(passwordRepeat);
        driver.findElement(By.cssSelector("#submit")).click();

        // 捕获错误
        driver.findElement(By.cssSelector("#signUpForm > div > div:nth-child(3) > div")); // 昵称不能为空

        // 用户名不填写 / 昵称填写 / 密码不填写 / 确认密码填写
        driver.findElement(By.cssSelector("#username")).clear();
        driver.findElement(By.cssSelector("#nickname")).clear();
        driver.findElement(By.cssSelector("#password")).clear();
        driver.findElement(By.cssSelector("#passwordRepeat")).clear();
        driver.findElement(By.cssSelector("#passwordRepeat")).sendKeys(passwordRepeat);
        driver.findElement(By.cssSelector("#submit")).click();

        // 捕获错误
        driver.findElement(By.cssSelector("#signUpForm > div > div:nth-child(2) > div")); // 用户名不能为空
        driver.findElement(By.cssSelector("#signUpForm > div > div:nth-child(4) > div > div")); // 密码不能为空

    }

    /**
     * 确认密码不一致
     */
    public void SignUpFail1 (String username,String nickname,String password,String passwordRepeat) {
        // 清空
        driver.findElement(By.cssSelector("#username")).clear();
        driver.findElement(By.cssSelector("#nickname")).clear();
        driver.findElement(By.cssSelector("#password")).clear();
        driver.findElement(By.cssSelector("#passwordRepeat")).clear();
        // 填入信息
        driver.findElement(By.cssSelector("#username")).sendKeys(username);
        driver.findElement(By.cssSelector("#nickname")).sendKeys(nickname);
        driver.findElement(By.cssSelector("#password")).sendKeys(password);
        driver.findElement(By.cssSelector("#passwordRepeat")).sendKeys(passwordRepeat);
        // 点击注册
        driver.findElement(By.cssSelector("#submit")).click();
        // 捕获错误
        driver.findElement(By.cssSelector("#signUpForm > div > div:nth-child(5) > div > div")); // 检查确认密码
    }

    /**
     * 用户名过长
     */
    public void SignUpFail2 (String username,String nickname,String password,String passwordRepeat) {
        // 清空
        driver.findElement(By.cssSelector("#username")).clear();
        driver.findElement(By.cssSelector("#nickname")).clear();
        driver.findElement(By.cssSelector("#password")).clear();
        driver.findElement(By.cssSelector("#passwordRepeat")).clear();
        // 填入信息
        driver.findElement(By.cssSelector("#username")).sendKeys(username);
        driver.findElement(By.cssSelector("#nickname")).sendKeys(nickname);
        driver.findElement(By.cssSelector("#password")).sendKeys(password);
        driver.findElement(By.cssSelector("#passwordRepeat")).sendKeys(passwordRepeat);
        // 点击注册
        driver.findElement(By.cssSelector("#submit")).click();
        // 捕获错误
        wait.until(ExpectedConditions.visibilityOfElementLocated(By.cssSelector("body > div.jq-toast-wrap.bottom-right > div")));
        String text = driver.findElement(By.xpath("/html/body/div[2]/div")).getText();
        String[] texts = text.split("\n");
        assert texts[texts.length - 1].equals("参数校验失败"); //  // 用户名过长导致警告
    }

    /**
     * 用户名重复
     */
    public void SignUpFail3 (String username,String nickname,String password,String passwordRepeat) {
        // 清空
        driver.findElement(By.cssSelector("#username")).clear();
        driver.findElement(By.cssSelector("#nickname")).clear();
        driver.findElement(By.cssSelector("#password")).clear();
        driver.findElement(By.cssSelector("#passwordRepeat")).clear();
        // 填入信息
        driver.findElement(By.cssSelector("#username")).sendKeys(username);
        driver.findElement(By.cssSelector("#nickname")).sendKeys(nickname);
        driver.findElement(By.cssSelector("#password")).sendKeys(password);
        driver.findElement(By.cssSelector("#passwordRepeat")).sendKeys(passwordRepeat);
        // 点击注册
        driver.findElement(By.cssSelector("#submit")).click();
        // 捕获错误
        wait.until(ExpectedConditions.visibilityOfElementLocated(By.cssSelector("body > div.jq-toast-wrap.bottom-right > div")));
        String text = driver.findElement(By.xpath("/html/body/div[2]/div")).getText();
        String[] texts = text.split("\n");
        assert texts[texts.length - 1].equals("参数校验失败"); // 用户名过长导致警告
    }
    public void SignUpSuccess (String username,String nickname,String password,String passwordRepeat) throws InterruptedException {
        // 清空
        driver.findElement(By.cssSelector("#username")).clear();
        driver.findElement(By.cssSelector("#nickname")).clear();
        driver.findElement(By.cssSelector("#password")).clear();
        driver.findElement(By.cssSelector("#passwordRepeat")).clear();
        // 填入正确信息
        driver.findElement(By.cssSelector("#username")).sendKeys(username);
        driver.findElement(By.cssSelector("#nickname")).sendKeys(nickname);
        driver.findElement(By.cssSelector("#password")).sendKeys(password);
        driver.findElement(By.cssSelector("#passwordRepeat")).sendKeys(passwordRepeat);
        driver.findElement(By.cssSelector("#submit")).click();
    }

    public void entrySignUp () {
        driver.get("http://127.0.0.1:58080/sign-up.html");
    }
}
